package com.smfx.yxt.controller.sys;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.TeacherWorkSign;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.TeacherWorkSignReqVO;
import com.smfx.yxt.service.ITeacherWorkSignService;
import com.smfx.yxt.utils.ExcelExportUtil;
import com.smfx.yxt.utils.ExportSetInfoUtil;

@RequestMapping("sys/teacher/workSign")
@Controller
public class SysTeacherWorkSignController extends BaseController {

	@Autowired
	private ITeacherWorkSignService teacherWorkSignService;
	
	@RequestMapping(value={"","/"})
	public String Index() {
		return "sys/teacherWorkSign";
	}
	
	@RequestMapping("pageQuery")
	@ResponseBody Object pageQuery(TeacherWorkSignReqVO vo){
		try {
			if(StringUtils.isNotBlank(vo.getTeacherType())) {
				String[] array = vo.getTeacherType().split(",");
				if(array.length == 1) {
					vo.setGroupCode(vo.getTeacherType());
				}else {
					vo.setGrade(Integer.valueOf(array[0]));
					vo.setStudyType(Integer.valueOf(array[1]));
				}
			}
			Page<TeacherWorkSign> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<OrderBy> orders = new ArrayList<>();
			if("signTimeText".equals(vo.getSort())){
				orders.add(new OrderBy("签到时间", vo.getOrder()));
			}else {
				orders.add(new OrderBy("签到时间", "asc"));
			}
			orders.add(new OrderBy("教师ID"));
			page.setSorts(orders);
			List<TeacherWorkSign> list = teacherWorkSignService.selectListByPage(page);
			Grid grid = new Grid();
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("export")
	ModelAndView workSignExport(TeacherWorkSignReqVO vo) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			if(StringUtils.isNotBlank(vo.getTeacherType())) {
				String[] array = vo.getTeacherType().split(",");
				if(array.length == 1) {
					vo.setGroupCode(vo.getTeacherType());
				}else {
					vo.setGrade(Integer.valueOf(array[0]));
					vo.setStudyType(Integer.valueOf(array[1]));
				}
			}
			Page<TeacherWorkSign> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(Integer.MAX_VALUE - 1);
			page.setParams(vo);
			List<OrderBy> orders = new ArrayList<>();
			if("signTimeText".equals(vo.getSort())){
				orders.add(new OrderBy("签到时间", vo.getOrder()));
			}else {
				orders.add(new OrderBy("签到时间", "asc"));
			}
			orders.add(new OrderBy("教师ID"));
			page.setSorts(orders);
			List<TeacherWorkSign> list = teacherWorkSignService.selectListByPage(page);
			//数据源
			LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
			String title="教师上班签到记录";
			map.put(title, list);
			
			//标题  和  标题变量
		    List<String[]> headNames = new ArrayList<String[]>();
		    headNames.add(new String[] { "日期","教师姓名","签到主题","签到状态","签到时间"});
		    List<String[]> fieldNames = new ArrayList<String[]>();
		    fieldNames.add(new String[] { "signDate","teacherName","signTopic","signStatus","signTimeText"});
		    
		    //excel生成帮助类
		    List<Integer[]> columnFormat=new ArrayList<Integer[]>();
		    Integer [] format=new Integer[22];
		    columnFormat.add(format);
		    
		    String [] titles=new String[] {title};
		    //excel生成帮助类
		    ExportSetInfoUtil setInfo = new ExportSetInfoUtil(map,titles,headNames,fieldNames,columnFormat);
		    request.setAttribute("exportDatas", setInfo);
		    
		    //生成
			modelAndView=new ModelAndView(new ExcelExportUtil(title+".xls",""));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}

	@RequestMapping("signTypeList")
	@ResponseBody Object signTypeList(){
		return teacherWorkSignService.selectSignTypeList();
	}
	
}
